package com.wechat.service.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * Created by jzm on 2017/7/24.
 */
@Service
public class BizRedisService {

    @Autowired
    private RedisService redisService;

    /**
     * 设置过期时间
     *
     * @param type
     * @param identity
     * @param expire
     * @return
     */
    public boolean expire(String type, String identity, long expire) {
        String key = handleParam(type, identity);

        return !"".equals(key) && redisService.expire(key, expire);
    }

    /**
     * 处理设置
     *
     * @param type
     * @param identity
     * @param value
     * @return
     */
    public boolean set(String type, String identity, String value) {
        String key = handleParam(type, identity);

        return !"".equals(key) && redisService.set(type + identity, value);
    }


    /**
     * 存储实体
     * @param type
     * @param identity
     * @param t
     * @param <T>
     * @return
     */
    public <T> boolean setEntity(String type, String identity, T t) {

        String key = handleParam(type, identity);

        return !"".equals(key) && redisService.setEntity(key, t);

    }

    /**
     * 获取实体
     * @param type
     * @param identity
     * @param clz
     * @param <T>
     * @return
     */
    public <T> T getEntity(String type, String identity, Class<T> clz) {
        String key = handleParam(type, identity);
        if ("".equals(key)) {
            return null;
        }
        return redisService.getEntity(key, clz);
    }

    /**
     * 获取对应的key值
     *
     * @param type
     * @param identity
     * @return
     */
    public String get(String type, String identity) {
        String key = handleParam(type, identity);
        if ("".equals(key)) {
            return null;
        }

        return redisService.get(key);
    }

    /**
     * 设置列表
     *
     * @param type
     * @param identity
     * @param list
     * @param <T>
     * @return
     */
    public <T> boolean setList(String type, String identity, List<T> list) {
        String key = handleParam(type, identity);

        return !"".equals(key) && redisService.setList(key, list);
    }

    /**
     * 获取key值列表
     *
     * @param type
     * @param identity
     * @param clz
     * @param <T>
     * @return
     */
    public <T> List<T> getList(String type, String identity, Class<T> clz) {
        String key = handleParam(type, identity);
        if ("".equals(key)) {
            return Collections.emptyList();
        }

        return redisService.getList(key, clz);
    }


    /**
     * 数组左边追加元素
     *
     * @param type
     * @param identity
     * @param obj
     * @return
     */
    public long lpush(String type, String identity, Object obj) {
        String key = handleParam(type, identity);
        if ("".equals(key)) {
            return -1;
        }

        return redisService.lpush(key, obj);
    }

    /**
     * 数组右边追加元素
     *
     * @param type
     * @param identity
     * @param obj
     * @return
     */
    public long rpush(String type, String identity, Object obj) {
        String key = handleParam(type, identity);
        if ("".equals(key)) {
            return -1;
        }

        return redisService.rpush(key, obj);
    }

    /**
     * 检测参数
     *
     * @param type
     * @param identity
     * @return
     */
    private String handleParam(String type, String identity) {
        if ("".equals(type)) {
            return "";
        }
        if ("".equals(identity)) {
            return "";
        }

        return type + "_" + identity;
    }
}
